from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time

d = webdriver.Chrome()
d.get('http://192.168.0.125:9009/')
time.sleep(1)

# 最大化窗口
d.maximize_window()

# 下拉条
js = "window.scrollTo(0,1000);"
d.execute_script(js)
time.sleep(1)

# 点击登录按钮
dd_ele = d.find_element(By.XPATH, '/html/body/div/div/div/aside/section[6]/ul/li[2]/a')
dd_ele.click()
time.sleep(1)

# 输入账号
y_ele = d.find_element(By.CSS_SELECTOR, 'input[name="log"]')
y_ele.send_keys('admin')
time.sleep(1)

# 输入密码
m_ele = d.find_element(By.CSS_SELECTOR, 'input[name="pwd"]')
m_ele.send_keys('admin123')
time.sleep(1)

# 点击登录按钮
d_ele = d.find_element(By.CSS_SELECTOR, 'input[name="wp-submit"]')
d_ele.click()
time.sleep(1)

# 鼠标在文章上悬停
wz_ele = d.find_element(By.CSS_SELECTOR, '[id="menu-users"] [class="wp-menu-name"]')
ActionChains(d).move_to_element(wz_ele).perform()
time.sleep(3)

# 点击添加用户
xwz_ele = d.find_element(By.LINK_TEXT, '添加用户')
xwz_ele.click()
time.sleep(1)

# 针对 select 标签的 下拉框，有单独的一套方案
# 1、先定位到下拉框
opts = d.find_element(By.CSS_SELECTOR, '[id="role"]')
# 2、导入 Select 模块
# Select(下拉框定位).select_by_index() # 根据下标选择
# Select(下拉框定位).select_by_value() # 根据 选项的 value 属性值 选择
# Select(下拉框定位).select_by_visible_text() # 根据 选项的 文本信息选择
Select(opts).select_by_index(3)  # 根据下标选择
time.sleep(1)
Select(opts).select_by_value('editor')  # 根据 选项的 value 属性值 选择
time.sleep(1)
Select(opts).select_by_visible_text('订阅者')  # 根据 选项的 文本信息选择
time.sleep(1)
